<template>
  <div class="product-list">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="编号" prop="id">
        <el-input 
          v-model="searchForm.id" 
          placeholder="请输入编号" 
          clearable 
        />
      </el-form-item>
      
      <el-form-item label="名称" prop="name">
        <el-input 
          v-model="searchForm.name" 
          placeholder="请输入类目名称" 
          clearable 
        />
      </el-form-item>
      
      <el-form-item label="批量查询" prop="ids">
        <el-input 
          v-model="searchForm.ids" 
          placeholder="请输入多个编号,以逗号分隔" 
          clearable 
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table :data="productList" class="data-table">
      <el-table-column fixed prop="id" label="编号" width="80" />
      <el-table-column prop="name" label="名称" width="100" />
      <el-table-column prop="subName" label="标题" width="120" />
      <el-table-column prop="img" label="图片" width="120">
        <template #default="{ row }">
          <img 
            :src="row.img.split(',')[0]" 
            alt="商品图片" 
            class="product-image"
          >
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="80" />
      <el-table-column prop="seq" label="排序" width="90" />
      <el-table-column prop="categoryId" label="父目录ID" width="90" />
      <el-table-column prop="statusX" label="状态" width="90" />
      <el-table-column prop="lastUpdateTime" label="最新时间" width="180" />
      
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="{ row }">
          <div class="action-buttons">
            <el-button 
              v-if="row.status !== 1" 
              type="success" 
              round 
              size="small" 
              @click="handleStatusChange(row, '上线', 1)"
            >
              上线
            </el-button>
            
            <el-button 
              v-if="row.status !== 5" 
              type="warning" 
              round 
              size="small" 
              @click="handleStatusChange(row, '下线', 5)"
            >
              下线
            </el-button>
            
            <el-button 
              v-if="row.status !== 10" 
              type="danger" 
              round 
              size="small" 
              @click="handleStatusChange(row, '禁售', 10)"
            >
              禁售
            </el-button>
            
            <el-button 
              type="primary" 
              round 
              size="small" 
              @click="handleEdit(row)"
            >
              修改
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      class="pagination"
      background
      layout="prev, pager, next"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ProductApi } from '@/api'

// 类型定义
interface SearchForm {
  id: string
  name: string
  ids: string[]
  categoryId: string
}

interface Pagination {
  total: number
  pageSize: number
  pageNum: number
}

interface ProductItem {
  id: number
  name: string
  status: number
  [key: string]: any
}

// 路由实例
const router = useRouter()

// 响应式数据
const productList = ref<ProductItem[]>([])

// 搜索表单数据
const searchForm = reactive<SearchForm>({
  id: '',
  name: '',
  ids: [],
  categoryId: ''
})

// 分页数据
const pagination = reactive<Pagination>({
  total: 0,
  pageSize: 5,
  pageNum: 1
})

// 生命周期钩子
onMounted(() => {
  loadProductList()
})

// 加载商品列表
const loadProductList = async () => {
  try {
    const params = {
      id: searchForm.id || undefined,
      name: searchForm.name || undefined,
      ids: searchForm.ids[0] ? searchForm.ids : undefined,
      pageNum: pagination.pageNum,
      pageSize: pagination.pageSize
    }

    const res = await ProductApi.select.call(params)
    productList.value = res.items
    pagination.total = res.total
  } catch (error) {
    ElMessage.error('加载商品列表失败')
  }
}

// 处理状态变更
const handleStatusChange = async (row: ProductItem, actionName: string, status: number) => {
  try {
    await ElMessageBox.confirm(
      `您确认要对 ${row.name} ${actionName}吗?`,
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }
    )

    await ProductApi.patch.call({ id: row.id, status })
    ElMessage.success(`${actionName}成功`)
    loadProductList()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error(`${actionName}失败`)
    }
  }
}

// 处理编辑
const handleEdit = (row: ProductItem) => {
  router.push({ 
    name: 'product-edit', 
    query: { id: row.id } 
  })
}

// 处理搜索
const handleSearch = () => {
  pagination.pageNum = 1
  loadProductList()
}

// 处理页码变化
const handlePageChange = (page: number) => {
  pagination.pageNum = page
  loadProductList()
}
</script>

<style scoped>
.product-list {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.data-table {
  margin-bottom: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.product-image {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 4px;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

:deep(.el-button) {
  padding: 8px 16px;
}

:deep(.el-input__wrapper) {
  border-radius: 4px;
}
</style>